D) Functor first&high order derivative

first derivative
#include <iostream>
#include <cmath>
using namespace std;
double sin_plus_cos(double x){
return sin(x)+cos(x);
}
class psc_f{
public:
psc_f(double alpha): alpha(alpha) {}
double operator()(double x) const{
return sin(alpha*(x))+cos(x);
}
private:
double alpha;
};
template <typename F, typename T>
T inline fin_diff(F f, const T& x, const T& h){
return (f(x+h)-f(x))/h;
}
int main(void){
psc_f psc_o(1.0);
std::cout<<fin_diff(psc_o, 1., 0.001)<<std::endl;
std::cout<<fin_diff(psc_f(2.0), 1., 0.001)<<std::endl;
std::cout<<fin_diff(sin_plus_cos, 0., 0.001)<<std::endl;
return 0;
}
second derivative
#include <iostream>
#include <cmath>
using namespace std;
class psc_f{
public:
psc_f(double alpha): alpha(alpha) {}
double operator()(double x) const{
return sin(alpha*(x))+cos(x);
}
private:
double alpha;
};
template <typename F, typename T>
class derivative{
public:
derivative(const F& f, const T& h): f(f), h(h) {}
T operator()(const T& x) const{
return (f(x+h)-f(x))/h;
}
private:
const F& f;
T h;
};
int main(void){
using d_psc_f=derivative<psc_f, double>;
using dd_psc_f=derivative<d_psc_f, double>;
psc_f psc_o(1.);
d_psc_f d_psc_o(psc_o, 0.001);
cout<<"der. of sin(0)+cos(0) is "<<d_psc_o(0.0)<<'\n';
dd_psc_f dd_psc_o(d_psc_o, 0.001);
cout<< "2nd der. of sin(0)+cos(0) is "<<dd_psc_o(0.0)<<'\n';
return 0;
}
second derivative without first derivative
2계 도함수 펑터내 생성자에서 1계 도함수를 나타내는 함수 개체를 생성
#include <iostream>
#include <cmath>
using namespace std;
class psc_f{
public:
psc_f(double alpha): alpha(alpha) {}
double operator()(double x) const{
return sin(alpha*(x))+cos(x);
}
private:
double alpha;
};
template <typename F, typename T>
class derivative{
public:
derivative(const F& f, const T& h): f(f), h(h) {}
T operator()(const T& x) const{
return (f(x+h)-f(x))/h;
}
private:
const F& f;
T h;
};
template <typename F, typename T>
class second_derivative{
public:
second_derivative(const F& f, const T& h): h(h), fp(f, h) {}
T operator()(const T& x) const{
return (fp(x+h)-fp(x))/h;
}
private:
T h;
derivative<F, T> fp;
};
int main(void){
second_derivative<psc_f, double> dd_psc_2_o(psc_f(1.), 0.001);
cout<<"2nd der. sin(0)+cos(0) is "<<dd_psc_2_o(0.0)<<'\n';
return 0;
}
재귀를 이용한 22계 도함수
#include <iostream>
#include <cmath>
using namespace std;
template <typename F, typename T, unsigned N>
class nth_derivative{
using prev_derivative=nth_derivative<F, T, N-1>;
public:
nth_derivative(const F& f, const T& h): h(h), fp(f, h) {}
T operator()(const T& x) const{
return N&1?(fp(x+h)-fp(x))/h:(fp(x)-fp(x-h))/h;
}
private:
T h;
prev_derivative fp;
};
// 릿
template <typename F, typename T>
class nth_derivative<F, T, 1>{
public:
nth_derivative(const F& f, const T& h): f(f), h(h) {}
T operator()(const T& x) const{
return (f(x+h)-f(x))/h;
}
private:
const F& f;
T h;
};
class psc_f{
public:
psc_f(double alpha): alpha(alpha) {}
double operator()(double x) const{
return sin(alpha*(x))+cos(x);
}
private:
double alpha;
};
int main(void){
nth_derivative<psc_f, double, 22> d22_psc_o(psc_f(1.0), 0.00001);
cout<<"22nd der. of sin(0)+cos(0) is "<<d22_psc_o(0.0)<<'\n';
return 0;
}